package top.yongyan0624.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import top.yongyan0624.common.lang.Result;
import top.yongyan0624.entity.ItemLost;
import top.yongyan0624.entity.PropertyPay;

import java.io.IOException;
import java.util.Arrays;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yongyan
 * @since 2021-09-09
 */
@RestController
@RequestMapping("/item/lost")
public class ItemLostController extends BaseController {
    @GetMapping("/list")
    public Result list(String title, String userId) {
        Page<ItemLost> pageData = itemLostService.page(getPage(), new QueryWrapper<ItemLost>()
                .eq(StrUtil.isNotBlank(userId), "user_id", userId).like(StrUtil.isNotBlank(title), "title", title)
                .orderByDesc("create_time"));

        pageData.getRecords().forEach(i -> {
            i.setUser(sysUserService.getById(i.getUserId()));
            i.setImgList(StrUtil.split(i.getImg(), ',', true, true));
        });

        return Result.success(pageData);
    }

    @GetMapping("/info/{id}")
    public Result list(@PathVariable("id") Long id) {
        ItemLost itemLost = itemLostService.getById(id);

        itemLost.setUser(sysUserService.getById(itemLost.getUserId()));
        itemLost.setImgList(StrUtil.split(itemLost.getImg(), ',', true, true));

        return Result.success(itemLost);
    }

    @PostMapping("/save")
    public Result save(@RequestPart ItemLost itemLost, @RequestPart MultipartFile[] imgFiles) {
        try {
            if (imgFiles.length <= 0) {
                return Result.fail("文件为空");
            }
            //文件不为空，处理文件，存储文件并生成随机虚拟路径文件名
            String fileName = handleFile("lost", itemLost.getUserId(), imgFiles);

            //设置img虚拟路径
            itemLost.setImg(fileName);

        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
            return Result.fail("图片上传失败！");
        }

        itemLostService.save(itemLost);

        itemLost.setUser(sysUserService.getById(itemLost.getUserId()));

        return Result.success(itemLost);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('itemLost:update')")
    public Result update(@RequestPart ItemLost itemLost, @RequestPart MultipartFile[] imgFiles){
        try {
            if (imgFiles.length > 0) {
                //文件不为空，处理文件，存储文件并生成随机虚拟路径文件名
                String fileName = handleFile("lost", itemLost.getUserId(), imgFiles);

                //设置img虚拟路径
                itemLost.setImg(fileName);
            }
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
            return Result.fail("图片上传失败！");
        }

        itemLostService.updateById(itemLost);

        itemLost.setUser(sysUserService.getById(itemLost.getUserId()));

        return Result.success(itemLost);
    }

    @Transactional
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('itemLost:delete')")
    public Result delete(@RequestBody Long[] ids) {
        itemLostService.removeByIds(Arrays.asList(ids));

        return Result.success("");
    }
}
